#define TRIG 9
#define ECHO 10

void setup() {
  Serial.begin(115200);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
}

void loop() {

  digitalWrite(TRIG, LOW);
  delayMicroseconds(5);

  digitalWrite(TRIG, HIGH);
  delayMicroseconds(15);
  digitalWrite(TRIG, LOW);

  long duration = pulseIn(ECHO, HIGH, 30000); // timeout added

  if(duration == 0){
    Serial.println("No echo");
  } else {
    long distance = duration * 0.034 / 2;
    Serial.println(distance);
  }

  delay(200);
}
